package cn.car91.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.IllegalCar;
import cn.car91.config.InitApplication;
import cn.car91.utils.ImageManager;

import com.lidroid.xutils.exception.DbException;

public class IllegalCarsListAdapter extends BaseAdapter {

    private List<IllegalCar> mycars = new ArrayList<IllegalCar>();
    private Context context;
    private LayoutInflater layoutInflater;
    private InitApplication car91Application;
    static ImageManager imageManager;

    public List<IllegalCar> getMycars() {
        return mycars;
    }

    public void setMycars(List<IllegalCar> mycars) {
        this.mycars = mycars;
    }

    public IllegalCarsListAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        imageManager = new ImageManager(context, "sdcard/car91/temp/cartype");
        car91Application = (InitApplication) context.getApplicationContext();
    }

    @Override
    public int getCount() {
        return mycars.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mycars.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return mycars.get(arg0).getCid();
    }

    private class IllegalCarItem {
        public TextView id;
        public ImageView carType;
        public TextView carNo;
        public TextView carCity;
        public ImageView carDelete;
        public ImageView carBeSure;
    }

    @Override
    public View getView(final int position, View arg1, ViewGroup arg2) {
        IllegalCarItem illegalCarItem = null;
        if (arg1 == null) {
            illegalCarItem = new IllegalCarItem();

            arg1 = layoutInflater.inflate(R.layout.view_car, null);
            illegalCarItem.id = (TextView) arg1.findViewById(R.id.my_car_id);
            illegalCarItem.carType = (ImageView) arg1.findViewById(R.id.my_car_type);
            illegalCarItem.carNo = (TextView) arg1.findViewById(R.id.my_car_no);
            illegalCarItem.carCity = (TextView) arg1.findViewById(R.id.my_car_city);
            illegalCarItem.carDelete = (ImageView) arg1.findViewById(R.id.car_delete);
            illegalCarItem.carBeSure = (ImageView) arg1.findViewById(R.id.car_besure);
            arg1.setTag(illegalCarItem);
        } else {
            illegalCarItem = (IllegalCarItem) arg1.getTag();
        }

        final IllegalCar illegalCar = (IllegalCar) getItem(position);
        try {
            illegalCarItem.id.setText(String.valueOf(illegalCar.getCid()));
        } catch (Exception e) {
        }
        illegalCarItem.carDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mycars.remove(illegalCar);
                notifyDataSetChanged();
                try {
                    car91Application.getDbUtils().delete(illegalCar);
                } catch (DbException e) {
                }
            }
        });
        illegalCarItem.carNo.setText(illegalCar.getCityshortname() + illegalCar.getNo());
        illegalCarItem.carCity.setText("查询地：" + illegalCar.getCityname());
        imageManager.displayImage(illegalCar.getCartype(), illegalCarItem.carType, context.getResources().getDrawable(R.drawable.user_car_default));

        return arg1;
    }
}
